home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / misc / SetRev.lha / SetRev.c next >
Encoding:
C/C++ Source or Header  |  1995-03-18  |  8.2 KB  |  450 lines

  1. /**
  2. ***  SetRev.c    Set revision number due to the Amiga guidelines
  3. ***
  4. ***  This is a small BumpRev replacement (yes, yet another :-).
  5. ***  Unlike BumpRev, UpRev and others this doesn't need to use
  6. ***  a special revision file. Instead it scans a source file for
  7. ***  certain patterns which are replaced.
  8. ***
  9. ***
  10. ***  Compiler: Dice 3.01
  11. ***
  12. ***  Computer: Amiga 1200
  13. ***
  14. ***  Author:    Jochen Wiedmann
  15. ***        Am Eisteich 9
  16. ***        72555 Metzingen
  17. ***        Germany
  18. ***
  19. ***        Phone: (49)7123 / 14881
  20. ***        Internet: jochen.wiedmann@zdv.uni-tuebingen.de
  21. ***
  22. ***
  23. ***  This program is in the public domain; use it as you want,
  24. ***  but WITHOUT ANY WARRANTY!
  25. **/
  26.  
  27.  
  28. /**
  29. ***  These are the patterns that will be replaced.
  30. **/
  31. #define VERSION        1
  32. #define REVISION       3
  33. #define DATE    "17.03.95"
  34. #define VERS    "SetRev 1.3"
  35. #define VSTRING "SetRev 1.3 (17.03.95)"
  36. #define VERSTAG "\0$VER: SetRev 1.3 (17.03.95)"
  37. const char *const VersionString=VERSTAG;
  38.  
  39.  
  40.  
  41.  
  42. /**
  43. ***  Include files
  44. **/
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <time.h>
  49. #include <errno.h>
  50.  
  51. #if defined(__GNUC__)
  52. #define stricmp strcasecmp
  53. #define strnicmp strncasecmp
  54. #endif
  55.  
  56.  
  57. #ifndef FALSE
  58. #define FALSE 0
  59. #endif
  60. #ifndef TRUE
  61. #define TRUE (!FALSE)
  62. #endif
  63.  
  64.  
  65.  
  66.  
  67.  
  68. /**
  69. ***  SetRev cannot scan files with lines longer than MAXLINE character.
  70. **/
  71. #define MAXLINE 4096
  72.  
  73.  
  74.  
  75.  
  76. /**
  77. ***  Global variables
  78. **/
  79. int CurrentVersion = 1;
  80. int CurrentRevision = 1;
  81. char *CurrentName;
  82. char CurrentDate[20];
  83.  
  84. char line[MAXLINE];
  85.  
  86. int Created = FALSE;
  87.  
  88.  
  89.  
  90. /**
  91. ***  This function determines the current name, version
  92. ***  and revision.
  93. **/
  94. void FirstScan(char *file)
  95.  
  96.   {
  97.     FILE *fh;
  98.     int linenum = 1;
  99.  
  100.     if (!(fh = fopen(file, "r")))
  101.       {
  102.     if (errno == ENOENT)
  103.       {
  104.         /**
  105.         ***  File doesn't exist, create it.
  106.         **/
  107.         if (!(fh = fopen(file, "w")))
  108.           {
  109.         perror("SetRev");
  110.         exit(10);
  111.           }
  112.  
  113.         if (fprintf(fh,
  114.                "#define VERSION     %d\n",
  115.                CurrentVersion)    <  0                ||
  116.         fprintf(fh,
  117.                "#define REVISION    %d\n",
  118.                CurrentRevision)  <  0                ||
  119.         fprintf(fh,
  120.                "#define DATE        \"%s\"\n",
  121.                CurrentDate)  <    0                ||
  122.         fprintf(fh,
  123.                "#define VERS        \"%s %d.%d\"\n",
  124.                CurrentName, CurrentVersion,
  125.                CurrentRevision)  <  0                ||
  126.         fprintf(fh,
  127.                "#define VSTRING     \"%s %d.%d (%s)\\r\\n\"\n",
  128.                CurrentName, CurrentVersion,
  129.                CurrentRevision, CurrentDate)  <  0        ||
  130.         fprintf(fh,
  131.                "#define VERSTAG     \"\\0%sVER: %s %d.%d (%s)\"\n",
  132.                "$", /*  Prevent "version" command from using the    */
  133.                 /*    above string.                    */
  134.                CurrentName, CurrentVersion,
  135.                CurrentRevision, CurrentDate)  <  0        ||
  136.         fclose(fh))
  137.           {
  138.         perror("SetRev");
  139.         exit(10);
  140.           }
  141.  
  142.         Created = TRUE;
  143.  
  144.         return;
  145.       }
  146.     perror("SetRev");
  147.     exit(10);
  148.       }
  149.  
  150.     errno = 0;
  151.     while(fgets(line, MAXLINE, fh)  &&  !errno)
  152.       {
  153.     int len = strlen(line);
  154.  
  155.     if (len+1 == MAXLINE  &&  line[MAXLINE-2] != '\n')
  156.       {
  157.         fprintf(stderr,
  158.             "SetRev warning: Line %d exceeds %d characters\n",
  159.             linenum, MAXLINE);
  160.       }
  161.  
  162.     if (strncmp(line, "#define", 7) == 0)
  163.       {
  164.         char *ptr = line+7;
  165.  
  166.         while (*ptr == ' '  ||  *ptr == '\t')
  167.           {
  168.         ++ptr;
  169.           }
  170.  
  171.         if (strncmp(ptr, "VERSION", 7) == 0  &&
  172.         (ptr[7] == ' ' || ptr[7] == '\t'))
  173.           {
  174.         CurrentVersion = atoi(&ptr[7]);
  175.           }
  176.         else if (strncmp(ptr, "REVISION", 8) == 0  &&
  177.              (ptr[8] == ' '  ||  ptr[8] == '\t'))
  178.           {
  179.         CurrentRevision = atoi(&ptr[8]);
  180.           }
  181.       }
  182.  
  183.     if (line[len-1] == '\n')
  184.       {
  185.         ++linenum;
  186.       }
  187.       }
  188.     fclose(fh);
  189.   }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. /**
  196. ***  This function inserts the new version, revision,
  197. ***  projectname and other stuff in a second scan.
  198. **/
  199. void SecondScan(char *file)
  200.  
  201.   {
  202.     char *bakfile;
  203.     FILE *fh, *bfh;
  204.     int linenum = 1;
  205.  
  206.     if (!(bakfile = malloc(strlen(file)+5)))
  207.       {
  208.     perror("SetRev");
  209.     exit(10);
  210.       }
  211.     strcpy(bakfile, file);
  212.     strcat(bakfile, ".bak");
  213.  
  214.     if (!(fh = fopen(file, "r"))  ||  !(bfh = fopen(bakfile, "w")))
  215.       {
  216.     perror("SetRev");
  217.     exit(10);
  218.       }
  219.  
  220.     errno = 0;
  221.     while(fgets(line, MAXLINE, fh)  &&  !errno)
  222.       {
  223.     int len = strlen(line);
  224.  
  225.     if (len+1 == MAXLINE  &&  line[MAXLINE-2] != '\n')
  226.       {
  227.         fprintf(stderr,
  228.             "SetRev warning: Line %d exceeds %d characters\n",
  229.             linenum, MAXLINE);
  230.       }
  231.  
  232.     if (strncmp(line, "#define", 7) == 0)
  233.       {
  234.         char *ptr = line+7;
  235.  
  236.         while (*ptr == ' '  ||  *ptr == '\t')
  237.           {
  238.         ++ptr;
  239.           }
  240.  
  241.         if (strncmp(ptr, "VERSION", 7) == 0  &&
  242.         (ptr[7] == ' ' || ptr[7] == '\t'))
  243.           {
  244.         ptr += 7;
  245.         while (*ptr == ' '  ||  *ptr == '\t')
  246.           {
  247.             ++ptr;
  248.           }
  249.         sprintf(ptr, "%d\n", CurrentVersion);
  250.           }
  251.         else if (strncmp(ptr, "REVISION", 8) == 0  &&
  252.              (ptr[8] == ' '  ||  ptr[8] == '\t'))
  253.           {
  254.         ptr += 8;
  255.         while (*ptr == ' '  ||  *ptr == '\t')
  256.           {
  257.             ++ptr;
  258.           }
  259.         sprintf(ptr, "%d\n", CurrentRevision);
  260.           }
  261.         else if (strncmp(ptr, "DATE", 4) == 0  &&
  262.              (ptr[4] == ' '  ||  ptr[4] == '\t'))
  263.           {
  264.         ptr += 4;
  265.         while (*ptr == ' '  ||  *ptr == '\t')
  266.           {
  267.             ++ptr;
  268.           }
  269.         sprintf(ptr, "\"%s\"\n", CurrentDate);
  270.           }
  271.         else if (strncmp(ptr, "VERS", 4) == 0  &&
  272.              (ptr[4] == ' '  ||  ptr[4] == '\t'))
  273.           {
  274.         ptr += 4;
  275.         while (*ptr == ' '  ||  *ptr == '\t')
  276.           {
  277.             ++ptr;
  278.           }
  279.         sprintf(ptr, "\"%s %d.%d\"\n", CurrentName,
  280.             CurrentVersion, CurrentRevision);
  281.           }
  282.         else if (strncmp(ptr, "VSTRING", 7) == 0  &&
  283.              (ptr[7] == ' '  ||  ptr[7] == '\t'))
  284.           {
  285.         ptr += 7;
  286.         while (*ptr == ' '  ||  *ptr == '\t')
  287.           {
  288.             ++ptr;
  289.           }
  290.         sprintf(ptr, "\"%s %d.%d (%s)\"\n",
  291.             CurrentName, CurrentVersion,
  292.             CurrentRevision, CurrentDate);
  293.           }
  294.         else if (strncmp(ptr, "VERSTAG", 7) == 0  &&
  295.              (ptr[7] == ' '  ||  ptr[7] == '\t'))
  296.           {
  297.         ptr += 7;
  298.         while (*ptr == ' '  ||  *ptr == '\t')
  299.           {
  300.             ++ptr;
  301.           }
  302.         sprintf(ptr, "\"\\0%sVER: %s %d.%d (%s)\"\n",
  303.             "$",
  304.             CurrentName, CurrentVersion,
  305.             CurrentRevision, CurrentDate);
  306.           }
  307.       }
  308.     fputs(line, bfh);
  309.       }
  310.     fclose(fh);
  311.     fclose(bfh);
  312.  
  313.     remove(file);
  314.     rename(bakfile, file);
  315.   }
  316.  
  317.  
  318.  
  319.  
  320.  
  321. /**
  322. ***  This prints out the Usage: message.
  323. **/
  324. void Usage(void)
  325.  
  326.   {
  327.     printf("Usage: SetRev PROJECT/A,VERSION/N,FILE/K\n\n");
  328.     printf("The given FILE (default: PROJECT_rev.h) will be searched for\n");
  329.     printf("version and revision definitions and bumped to the next\n");
  330.     printf("revision number.\n\n");
  331.     printf("%s © 1994  by  Jochen Wiedmann\n\n", VSTRING);
  332.     printf("This program is in the public domain, use it as you want, but\n");
  333.     printf("WITHOUT ANY WARRANTY.\n");
  334.     exit(5);
  335.   }
  336.  
  337.  
  338.  
  339.  
  340.  
  341. /**
  342. ***  Finally main()
  343. **/
  344. int main(int argc, char *argv[])
  345.  
  346.   {
  347.     int i;
  348.     char *file;
  349.     int versionseen, version;
  350.     time_t currenttime = time(NULL);
  351.     struct tm *localtm = localtime(¤ttime);
  352.  
  353.     /**
  354.     *** Get local time
  355.     **/
  356.     currenttime = time(NULL);
  357.     localtm = localtime(¤ttime);
  358.     strftime(CurrentDate, sizeof(CurrentDate), "%d.%m.%y", localtm);
  359.  
  360.     if (argc < 2  ||
  361.     strcmp(argv[1], "?") == 0  ||
  362.     strcmp(argv[1], "-h") == 0  ||
  363.     strcmp(argv[1], "--help") == 0)
  364.       {
  365.     Usage();
  366.       }
  367.  
  368.     CurrentName = NULL;
  369.     versionseen = FALSE;
  370.     file = NULL;
  371.     for (i = 1;  i < argc;  i++)
  372.       {
  373.     if (stricmp(argv[i], "file") == 0  &&  i+1 < argc)
  374.       {
  375.         if (file)
  376.           {
  377.         Usage();
  378.           }
  379.         file = argv[++i];
  380.       }
  381.     else if (strnicmp(argv[i], "file=", 5) == 0)
  382.       {
  383.         if (file)
  384.           {
  385.         Usage();
  386.           }
  387.         file = &argv[i][5];
  388.       }
  389.     else if (!CurrentName)
  390.       {
  391.         CurrentName = argv[i];
  392.       }
  393.     else if (!versionseen)
  394.       {
  395.         if ((version = atoi(argv[i])) == 0)
  396.           {
  397.         Usage();
  398.           }
  399.         versionseen = TRUE;
  400.       }
  401.     else
  402.       {
  403.         Usage();
  404.       }
  405.       }
  406.     if (!CurrentName)
  407.       {
  408.     Usage();
  409.       }
  410.  
  411.     if (!file)
  412.       {
  413.     if (!(file = malloc(strlen(CurrentName) + 7)))
  414.       {
  415.         errno = ENOMEM;
  416.         perror("SetRev");
  417.         exit(20);
  418.       }
  419.     strcpy(file, CurrentName);
  420.     strcat(file, "_rev.h");
  421.       }
  422.  
  423.     if (versionseen)
  424.       {
  425.     CurrentVersion = version;
  426.       }
  427.  
  428.     FirstScan(file);
  429.  
  430.     if (!Created)
  431.       {
  432.     if (versionseen  &&  version != CurrentVersion)
  433.       {
  434.         CurrentRevision = 1;
  435.       }
  436.     else
  437.       {
  438.         ++CurrentRevision;
  439.       }
  440.  
  441.     if (versionseen)
  442.       {
  443.         CurrentVersion = version;
  444.       }
  445.  
  446.     SecondScan(file);
  447.       }
  448.     exit(0);
  449.   }
  450.